/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.upn.sigemfi.vfi.bc;

import edu.upn.sigemfi.ope.be.Ventanilla;
import edu.upn.sigemfi.vfi.be.Caja;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 *
 * @author Alex
 */
public class CajaDelegate {

        private CajaFacadeLocal local;

    public CajaDelegate() {
        local = lookupCajaFacade();
    }

    public void crear(Caja caja) throws Exception {
        try {
            local.create(caja);
        } catch (Exception e) {
            local = lookupCajaFacade();
            throw new Exception("Crear Caja. " + e.getMessage(), e);
        }
    }

    public Caja buscarPorVentanilla(Ventanilla fidOpeVentnailla ) throws Exception
    {

           try {
            return local.buscarPorVentanilla(fidOpeVentnailla);
        } catch (Exception e) {
            local = lookupCajaFacade();
            throw new Exception("Buscar Caja por Ventanilla. " + e.getMessage(), e);
        }
    }

    public void editar(Caja caja) throws Exception {
        try {
            local.edit(caja);
        } catch (Exception e) {
            local = lookupCajaFacade();
            throw new Exception("Editar Caja. " + e.getMessage(), e);
        }
    }

    public void eliminar(Caja caja) throws Exception {
        try {
            local.remove(caja);
        } catch (Exception e) {
            local = lookupCajaFacade();
            throw new Exception("Eliminar Caja. " + e.getMessage(), e);
        }
    }

    public Caja buscar(Long codigo) throws Exception {
        try {
            return local.find(codigo);
        } catch (Exception e) {
            local = lookupCajaFacade();
            throw new Exception("Buscar Caja. " + e.getMessage(), e);
        }
    }

    public List<Caja> listar() throws Exception {
        try {
            return local.findAll();
        } catch (Exception e) {
            local = lookupCajaFacade();
            throw new Exception("Listar Caja. " + e.getMessage(), e);
        }
    }

    private CajaFacadeLocal lookupCajaFacade() {
        try {
            Context c = new InitialContext();
            return (CajaFacadeLocal) c.lookup("java:comp/env/CajaFacade");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }



}
